package com.atguigu.servlet;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/servlet1")
public class Servlet1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Servlet 1 service");
        // 向应用域中放入数据
        ServletContext context = this.getServletContext();
        context.setAttribute("keyA", "valueA");

        // 创建session举例
        HttpSession session = req.getSession();

        session.setAttribute("keyB", "valueB");
        session.setAttribute("keyC", "valueC");
        session.removeAttribute("keyC");
        session.setAttribute("keyB", "valueYYY");

        // 手动销毁session举例
        session.invalidate();
    }
}
